import * as Contant from '@/constant/index.js';
// H5微信分享
export const wx_h5_share = (wx, data) => {
  wx.ready(() => {
    // console.log('wx.ready:',data);
    if (data) {
      wx.onMenuShareAppMessage({
        title: data.title, // 分享标题
        desc: data.desc, // 分享描述
        link: data.link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: data.imgUrl, // 分享图标
        type: '', // 分享类型,music、video或link，不填默认为link
        dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
        success: () => {
          // 用户点击了分享后执行的回调函数
        },
      });
      wx.onMenuShareTimeline({
        title: data.title, // 分享标题
        link: data.link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: data.imgUrl, // 分享图标
        success: () => {
          // 用户点击了分享后执行的回调函数
        },
      });
    } else {
      wx.hideAllNonBaseMenuItem();
      function onBridgeReady() {
        WeixinJSBridge.call('hideOptionMenu');
      }
      if (typeof WeixinJSBridge == 'undefined') {
        if (document.addEventListener) {
          document.addEventListener(
            'WeixinJSBridgeReady',
            onBridgeReady,
            false,
          );
        } else if (document.attachEvent) {
          document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
          document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
        }
      } else {
        onBridgeReady();
      }
    }
  });
};

export const shareUrl =
  process.env.NODE_ENV === 'development'
    ? Contant.DEV_FRONTURL
    : Contant.PRO_FRONTURL;
export const wx_AuthUrl = (url) => {
  let urlTmp = `${shareUrl}/#/`;

  if (url) {
    urlTmp = `${urlTmp}${url}`;
  }

  urlTmp = encodeURIComponent(urlTmp);

  return `https://gateway.wxls.pro/topic/apiPass/wechat/authorize?returnUrl=${urlTmp}`;
};
